// pages/acupoint-3d/acupoint-3d.js
Page({
  data: {
    // 搜索相关
    searchKeyword: '',
    searchResults: [],
    showSearchResults: false,
    highlightedAcupoint: null, // 高亮显示的穴位
    searchTimer: null, // 搜索防抖定时器
    
    // 3D显示相关
    currentView: 'front', // front, side, back
    currentViewImage: 'http://39.97.48.169:9000/zhongyi/e861db718e094fcab304882b42fdde31.png',
    is3DMode: false,
    zoomLevel: 1,
    rotationX: 0,
    rotationY: 0,
    showControlPanel: false,
    
    // 穴位数据 - 精确的中医理论数据
    acupoints: [
      {
        id: 1,
        name: '大椎穴',
        pinyin: 'dazhui',
        meridian: '督脉',
        position: '第7颈椎棘突下凹陷中',
        description: '大椎穴是督脉上的重要穴位，具有清热解表、通阳理气、扶正祛邪的功效',
        coordinates: { x: 50, y: 16 },
        viewImages: {
          front: 'http://39.97.48.169:9000/zhongyi/e861db718e094fcab304882b42fdde31.png',
          side: 'http://39.97.48.169:9000/zhongyi/71b0c8210d1443cbb9f3485ca13a0484.png',
          back: 'http://39.97.48.169:9000/zhongyi/67de2162e4624d5fb0f05f7f04ff0a08.png'
        }
      },
      {
        id: 2,
        name: '百会穴',
        pinyin: 'baihui',
        meridian: '督脉',
        position: '头顶正中线与两耳尖连线的交点处',
        description: '百会穴是督脉上的重要穴位，具有升阳举陷、开窍醒神、安神定志的功效',
        coordinates: { x: 50, y: 6 },
        viewImages: {
          front: 'http://39.97.48.169:9000/zhongyi/e861db718e094fcab304882b42fdde31.png',
          side: 'http://39.97.48.169:9000/zhongyi/71b0c8210d1443cbb9f3485ca13a0484.png',
          back: 'http://39.97.48.169:9000/zhongyi/67de2162e4624d5fb0f05f7f04ff0a08.png'
        }
      },
      {
        id: 3,
        name: '足三里',
        pinyin: 'zusanli',
        meridian: '足阳明胃经',
        position: '犊鼻下3寸，胫骨前缘一横指处',
        description: '足三里是足阳明胃经上的重要穴位，具有健脾和胃、扶正培元、调理气血的功效',
        coordinates: { x: 45, y: 70 },
        viewImages: {
          front: 'http://39.97.48.169:9000/zhongyi/e861db718e094fcab304882b42fdde31.png',
          side: 'http://39.97.48.169:9000/zhongyi/71b0c8210d1443cbb9f3485ca13a0484.png',
          back: 'http://39.97.48.169:9000/zhongyi/67de2162e4624d5fb0f05f7f04ff0a08.png'
        }
      },
      {
        id: 4,
        name: '合谷穴',
        pinyin: 'hegu',
        meridian: '手阳明大肠经',
        position: '手背第1、2掌骨间，第2掌骨桡侧中点处',
        description: '合谷穴是手阳明大肠经上的重要穴位，具有疏风解表、通络止痛、清热泻火的功效',
        coordinates: { x: 25, y: 60 },
        viewImages: {
          front: 'http://39.97.48.169:9000/zhongyi/e861db718e094fcab304882b42fdde31.png',
          side: 'http://39.97.48.169:9000/zhongyi/71b0c8210d1443cbb9f3485ca13a0484.png',
          back: 'http://39.97.48.169:9000/zhongyi/67de2162e4624d5fb0f05f7f04ff0a08.png'
        }
      },
      {
        id: 5,
        name: '内关穴',
        pinyin: 'neiguan',
        meridian: '手厥阴心包经',
        position: '前臂掌侧，腕横纹上2寸，掌长肌腱与桡侧腕屈肌腱之间',
        description: '内关穴是手厥阴心包经上的重要穴位，具有宁心安神、理气止痛、和胃降逆的功效',
        coordinates: { x: 30, y: 50 },
        viewImages: {
          front: 'http://39.97.48.169:9000/zhongyi/e861db718e094fcab304882b42fdde31.png',
          side: 'http://39.97.48.169:9000/zhongyi/71b0c8210d1443cbb9f3485ca13a0484.png',
          back: 'http://39.97.48.169:9000/zhongyi/67de2162e4624d5fb0f05f7f04ff0a08.png'
        }
      },
      {
        id: 6,
        name: '中脘穴',
        pinyin: 'zhongwan',
        meridian: '任脉',
        position: '前正中线上，脐上4寸',
        description: '中脘穴是任脉上的重要穴位，具有和胃降逆、健脾化湿、理气止痛的功效',
        coordinates: { x: 50, y: 38 },
        viewImages: {
          front: 'http://39.97.48.169:9000/zhongyi/e861db718e094fcab304882b42fdde31.png',
          side: 'http://39.97.48.169:9000/zhongyi/71b0c8210d1443cbb9f3485ca13a0484.png',
          back: 'http://39.97.48.169:9000/zhongyi/67de2162e4624d5fb0f05f7f04ff0a08.png'
        }
      },
      {
        id: 7,
        name: '神门穴',
        pinyin: 'shenmen',
        meridian: '手少阴心经',
        position: '腕横纹尺侧端，尺侧腕屈肌腱的桡侧凹陷处',
        description: '神门穴是手少阴心经上的重要穴位，具有宁心安神、清心调气、通络止痛的功效',
        coordinates: { x: 35, y: 52 },
        viewImages: {
          front: 'http://39.97.48.169:9000/zhongyi/e861db718e094fcab304882b42fdde31.png',
          side: 'http://39.97.48.169:9000/zhongyi/71b0c8210d1443cbb9f3485ca13a0484.png',
          back: 'http://39.97.48.169:9000/zhongyi/67de2162e4624d5fb0f05f7f04ff0a08.png'
        }
      },
      {
        id: 8,
        name: '涌泉穴',
        pinyin: 'yongquan',
        meridian: '足少阴肾经',
        position: '足底前1/3处，足趾跖屈时呈凹陷处',
        description: '涌泉穴是足少阴肾经上的重要穴位，具有滋阴降火、宁神开窍、补肾益精的功效',
        coordinates: { x: 50, y: 85 },
        viewImages: {
          front: 'http://39.97.48.169:9000/zhongyi/e861db718e094fcab304882b42fdde31.png',
          side: 'http://39.97.48.169:9000/zhongyi/71b0c8210d1443cbb9f3485ca13a0484.png',
          back: 'http://39.97.48.169:9000/zhongyi/67de2162e4624d5fb0f05f7f04ff0a08.png'
        }
      },
      {
        id: 9,
        name: '太阳穴',
        pinyin: 'taiyang',
        meridian: '经外奇穴',
        position: '眉梢与目外眦之间，向后约一横指的凹陷处',
        description: '太阳穴是经外奇穴，具有疏风清热、明目止痛、通络开窍的功效',
        coordinates: { x: 40, y: 20 },
        viewImages: {
          front: 'http://39.97.48.169:9000/zhongyi/e861db718e094fcab304882b42fdde31.png',
          side: 'http://39.97.48.169:9000/zhongyi/71b0c8210d1443cbb9f3485ca13a0484.png',
          back: 'http://39.97.48.169:9000/zhongyi/67de2162e4624d5fb0f05f7f04ff0a08.png'
        }
      },
      {
        id: 10,
        name: '风池穴',
        pinyin: 'fengchi',
        meridian: '足少阳胆经',
        position: '胸锁乳突肌与斜方肌上端之间的凹陷中',
        description: '风池穴是足少阳胆经上的重要穴位，具有疏风清热、开窍醒神、通络止痛的功效',
        coordinates: { x: 45, y: 26 },
        viewImages: {
          front: 'http://39.97.48.169:9000/zhongyi/e861db718e094fcab304882b42fdde31.png',
          side: 'http://39.97.48.169:9000/zhongyi/71b0c8210d1443cbb9f3485ca13a0484.png',
          back: 'http://39.97.48.169:9000/zhongyi/67de2162e4624d5fb0f05f7f04ff0a08.png'
        }
      },
      {
        id: 11,
        name: '鱼际穴',
        pinyin: 'yuji',
        meridian: '手太阴肺经',
        position: '第1掌骨中点桡侧，赤白肉际处',
        description: '鱼际穴是手太阴肺经上的重要穴位，具有清热利咽、止咳平喘、通络止痛的功效',
        coordinates: { x: 60, y: 68 },
        viewImages: {
          front: 'http://39.97.48.169:9000/zhongyi/e861db718e094fcab304882b42fdde31.png',
          side: 'http://39.97.48.169:9000/zhongyi/71b0c8210d1443cbb9f3485ca13a0484.png',
          back: 'http://39.97.48.169:9000/zhongyi/67de2162e4624d5fb0f05f7f04ff0a08.png'
        }
      },
      {
        id: 12,
        name: '身柱穴',
        pinyin: 'shenzhu',
        meridian: '督脉',
        position: '第3胸椎棘突下凹陷中',
        description: '身柱穴是督脉上的重要穴位，具有宣肺止咳、宁心安神、通络止痛的功效',
        coordinates: { x: 50, y: 22 },
        viewImages: {
          front: 'http://39.97.48.169:9000/zhongyi/e861db718e094fcab304882b42fdde31.png',
          side: 'http://39.97.48.169:9000/zhongyi/71b0c8210d1443cbb9f3485ca13a0484.png',
          back: 'http://39.97.48.169:9000/zhongyi/67de2162e4624d5fb0f05f7f04ff0a08.png'
        }
      },
      {
        id: 13,
        name: '至阳穴',
        pinyin: 'zhiyang',
        meridian: '督脉',
        position: '第7胸椎棘突下凹陷中',
        description: '至阳穴是督脉上的重要穴位，具有宽胸理气、清热利湿、通络止痛的功效',
        coordinates: { x: 50, y: 28 },
        viewImages: {
          front: 'http://39.97.48.169:9000/zhongyi/e861db718e094fcab304882b42fdde31.png',
          side: 'http://39.97.48.169:9000/zhongyi/71b0c8210d1443cbb9f3485ca13a0484.png',
          back: 'http://39.97.48.169:9000/zhongyi/67de2162e4624d5fb0f05f7f04ff0a08.png'
        }
      },
      {
        id: 14,
        name: '命门穴',
        pinyin: 'mingmen',
        meridian: '督脉',
        position: '第2腰椎棘突下凹陷中',
        description: '命门穴是督脉上的重要穴位，具有温肾壮阳、强腰健肾、固精止遗的功效',
        coordinates: { x: 50, y: 38 },
        viewImages: {
          front: 'http://39.97.48.169:9000/zhongyi/e861db718e094fcab304882b42fdde31.png',
          side: 'http://39.97.48.169:9000/zhongyi/71b0c8210d1443cbb9f3485ca13a0484.png',
          back: 'http://39.97.48.169:9000/zhongyi/67de2162e4624d5fb0f05f7f04ff0a08.png'
        }
      }
    ],
    
    // 当前选中的穴位
    selectedAcupoint: null,
    
    // 经络穴位数据
    meridianPoints: [],
    
    // 经络穴位数据库
    meridianDatabase: {
      '手太阴肺经': [
        { name: '中府', position: '胸前壁外上方，第1肋间隙' },
        { name: '云门', position: '胸前壁外上方，锁骨下窝凹陷处' },
        { name: '天府', position: '上臂内侧，腋前纹头下3寸' },
        { name: '侠白', position: '上臂内侧，天府下1寸' },
        { name: '尺泽', position: '肘横纹中，肱二头肌腱桡侧缘' },
        { name: '孔最', position: '前臂掌面桡侧，尺泽下5寸' },
        { name: '列缺', position: '前臂掌面桡侧，桡骨茎突上方' },
        { name: '经渠', position: '前臂掌面桡侧，桡骨茎突与桡动脉之间' },
        { name: '太渊', position: '腕掌侧横纹桡侧，桡动脉搏动处' },
        { name: '鱼际', position: '第1掌骨中点桡侧，赤白肉际处' },
        { name: '少商', position: '拇指桡侧指甲角旁0.1寸' }
      ],
      '督脉': [
        { name: '百会', position: '头顶正中线与两耳尖连线的交点处' },
        { name: '大椎', position: '第7颈椎棘突下凹陷中' },
        { name: '身柱', position: '第3胸椎棘突下凹陷中' },
        { name: '至阳', position: '第7胸椎棘突下凹陷中' },
        { name: '命门', position: '第2腰椎棘突下凹陷中' }
      ],
      '足阳明胃经': [
        { name: '承泣', position: '目正视，瞳孔直下，当眼球与眶下缘之间' },
        { name: '四白', position: '目正视，瞳孔直下，当眶下孔凹陷处' },
        { name: '巨髎', position: '目正视，瞳孔直下，平鼻翼下缘处' },
        { name: '地仓', position: '口角旁0.4寸' },
        { name: '大迎', position: '下颌角前1.3寸' },
        { name: '颊车', position: '下颌角前上方一横指' },
        { name: '下关', position: '颧弓下缘凹陷处' },
        { name: '头维', position: '额角发际上0.5寸，头正中线旁4.5寸' },
        { name: '人迎', position: '喉结旁1.5寸，当颈总动脉搏动处' },
        { name: '水突', position: '人迎穴下1寸，胸锁乳突肌前缘' },
        { name: '气舍', position: '人迎穴直下，锁骨上缘' },
        { name: '足三里', position: '犊鼻下3寸，胫骨前缘一横指处' }
      ],
      '手阳明大肠经': [
        { name: '商阳', position: '食指桡侧指甲角旁0.1寸' },
        { name: '二间', position: '食指桡侧，第2掌指关节前凹陷处' },
        { name: '三间', position: '食指桡侧，第2掌指关节后凹陷处' },
        { name: '合谷', position: '手背第1、2掌骨间，第2掌骨桡侧中点处' },
        { name: '阳溪', position: '腕背横纹桡侧，拇短伸肌腱与拇长伸肌腱之间' },
        { name: '偏历', position: '前臂背面桡侧，阳溪上3寸' },
        { name: '温溜', position: '前臂背面桡侧，阳溪上5寸' },
        { name: '下廉', position: '前臂背面桡侧，阳溪上8寸' },
        { name: '上廉', position: '前臂背面桡侧，阳溪上9寸' },
        { name: '手三里', position: '前臂背面桡侧，阳溪上10寸' },
        { name: '曲池', position: '肘横纹外侧端，屈肘时肘横纹桡侧端' }
      ],
      '手厥阴心包经': [
        { name: '天池', position: '第4肋间隙，乳头外1寸' },
        { name: '天泉', position: '上臂内侧，腋前纹头下2寸' },
        { name: '曲泽', position: '肘横纹中，肱二头肌腱尺侧缘' },
        { name: '郄门', position: '前臂掌侧，曲泽下5寸' },
        { name: '间使', position: '前臂掌侧，曲泽下3寸' },
        { name: '内关', position: '前臂掌侧，腕横纹上2寸，掌长肌腱与桡侧腕屈肌腱之间' },
        { name: '大陵', position: '腕掌侧横纹中，掌长肌腱与桡侧腕屈肌腱之间' },
        { name: '劳宫', position: '手掌心，第2、3掌骨间，偏于第3掌骨' },
        { name: '中冲', position: '中指尖端中央' }
      ],
      '手少阴心经': [
        { name: '极泉', position: '腋窝顶点，腋动脉搏动处' },
        { name: '青灵', position: '上臂内侧，极泉下3寸' },
        { name: '少海', position: '肘横纹内侧端，肱骨内上髁前缘' },
        { name: '灵道', position: '前臂掌侧，少海上1.5寸' },
        { name: '通里', position: '前臂掌侧，少海上1寸' },
        { name: '阴郄', position: '前臂掌侧，少海上0.5寸' },
        { name: '神门', position: '腕横纹尺侧端，尺侧腕屈肌腱的桡侧凹陷处' },
        { name: '少府', position: '手掌面，第4、5掌骨间，偏于第4掌骨' },
        { name: '少冲', position: '小指桡侧指甲角旁0.1寸' }
      ],
      '足少阴肾经': [
        { name: '涌泉', position: '足底前1/3处，足趾跖屈时呈凹陷处' },
        { name: '然谷', position: '足舟骨粗隆下缘凹陷处' },
        { name: '太溪', position: '内踝尖与跟腱之间凹陷处' },
        { name: '大钟', position: '内踝后下方，跟腱附着部内侧凹陷处' },
        { name: '水泉', position: '内踝后下方，太溪下1寸' },
        { name: '照海', position: '内踝尖下1寸，内踝下缘凹陷处' },
        { name: '复溜', position: '小腿内侧，太溪上2寸' },
        { name: '交信', position: '小腿内侧，太溪上2寸，胫骨内侧缘后方' },
        { name: '筑宾', position: '小腿内侧，太溪上5寸' },
        { name: '阴谷', position: '腘窝内侧，半腱肌腱与半膜肌腱之间' }
      ],
      '足少阳胆经': [
        { name: '瞳子髎', position: '目外眦旁0.5寸，眶骨外缘凹陷处' },
        { name: '听会', position: '耳屏间切迹前，下颌骨髁状突后缘' },
        { name: '上关', position: '耳前，颧弓上缘凹陷处' },
        { name: '颔厌', position: '头维与曲鬓弧形连线的上1/4与下3/4交点处' },
        { name: '悬颅', position: '头维与曲鬓弧形连线的中点处' },
        { name: '悬厘', position: '头维与曲鬓弧形连线的下1/4与上3/4交点处' },
        { name: '曲鬓', position: '耳前鬓发后缘直上，与耳尖水平线交点处' },
        { name: '率谷', position: '耳尖直上，入发际1.5寸' },
        { name: '天冲', position: '耳根后缘直上，入发际2寸' },
        { name: '浮白', position: '耳根后缘直上，入发际1寸' },
        { name: '头窍阴', position: '耳根后缘直上，入发际0.5寸' },
        { name: '完骨', position: '耳后，乳突后下方凹陷处' },
        { name: '本神', position: '前发际上0.5寸，神庭旁开3寸' },
        { name: '阳白', position: '目正视，瞳孔直上，眉上1寸' },
        { name: '头临泣', position: '目正视，瞳孔直上，入发际0.5寸' },
        { name: '目窗', position: '头临泣后1寸' },
        { name: '正营', position: '目窗后1寸' },
        { name: '承灵', position: '正营后1.5寸' },
        { name: '脑空', position: '承灵后1.5寸' },
        { name: '风池', position: '胸锁乳突肌与斜方肌上端之间的凹陷中' },
        { name: '肩井', position: '肩上，大椎与肩峰连线的中点' },
        { name: '渊腋', position: '腋中线上，第4肋间隙' },
        { name: '辄筋', position: '渊腋前1寸，第4肋间隙' },
        { name: '日月', position: '乳头直下，第7肋间隙' },
        { name: '京门', position: '第12肋游离端下缘' },
        { name: '带脉', position: '第11肋游离端下缘' },
        { name: '五枢', position: '髂前上棘前0.5寸' },
        { name: '维道', position: '髂前上棘前下0.5寸' },
        { name: '居髎', position: '髂前上棘与股骨大转子连线的中点' },
        { name: '环跳', position: '股骨大转子与骶管裂孔连线的外1/3与内2/3交点处' },
        { name: '风市', position: '大腿外侧中线，腘横纹上7寸' },
        { name: '中渎', position: '大腿外侧中线，腘横纹上5寸' },
        { name: '膝阳关', position: '股骨外上髁上方的凹陷处' },
        { name: '阳陵泉', position: '腓骨小头前下方凹陷处' },
        { name: '阳交', position: '外踝尖上7寸，腓骨后缘' },
        { name: '外丘', position: '外踝尖上7寸，腓骨前缘' },
        { name: '光明', position: '外踝尖上5寸，腓骨前缘' },
        { name: '阳辅', position: '外踝尖上4寸，腓骨前缘' },
        { name: '悬钟', position: '外踝尖上3寸，腓骨前缘' },
        { name: '丘墟', position: '外踝前下方，趾长伸肌腱外侧凹陷处' },
        { name: '足临泣', position: '足背外侧，第4、5跖骨结合部前方' },
        { name: '地五会', position: '足背外侧，第4、5跖骨间，第4跖趾关节近端' },
        { name: '侠溪', position: '足背外侧，第4、5趾间，趾蹼缘后方赤白肉际处' },
        { name: '足窍阴', position: '第4趾外侧指甲角旁0.1寸' }
      ]
    }
  },

  onLoad(options) {
    console.log('3D穴位展示页面加载', options)
    this.init3DView()
    
    // 如果传入了穴位参数，自动选中该穴位
    if (options.acupoint) {
      const acupointName = decodeURIComponent(options.acupoint)
      console.log('自动选中穴位:', acupointName)
      this.autoSelectAcupoint(acupointName)
    }
  },

  // 自动选中穴位
  autoSelectAcupoint(acupointName) {
    const acupoint = this.data.acupoints.find(item => 
      item.name === acupointName || 
      item.name.includes(acupointName) ||
      acupointName.includes(item.name)
    )
    
    if (acupoint) {
      console.log('找到匹配的穴位:', acupoint)
      
      // 获取对应经络的穴位列表
      const meridianPoints = this.data.meridianDatabase[acupoint.meridian] || []
      
      this.setData({
        selectedAcupoint: acupoint,
        meridianPoints: meridianPoints
      })
      
      // 显示穴位信息，让用户选择是否进入3D模式
      wx.showModal({
        title: acupoint.name,
        content: `${acupoint.meridian}\n${acupoint.position}\n\n${acupoint.description}`,
        showCancel: false,
        confirmText: '确定',
        success: (res) => {
          if (res.confirm) {
            // 直接显示穴位信息，不进入3D模式
            this.setData({
              selectedAcupoint: acupoint
            })
            
            // 获取对应经络的穴位列表
            const meridianPoints = this.data.meridianDatabase[acupoint.meridian] || []
            this.setData({
              meridianPoints: meridianPoints
            })
          }
        }
      })
    } else {
      console.warn('未找到匹配的穴位:', acupointName)
      wx.showToast({
        title: `未找到穴位: ${acupointName}`,
        icon: 'none'
      })
    }
  },

  // 初始化3D视图
  init3DView() {
    console.log('初始化3D视图')
    // 设置默认视图图片
    this.setData({
      currentViewImage: this.data.acupoints[0].viewImages.front
    })
  },

  // 进入3D模式
  enter3DMode(acupoint) {
    console.log('进入3D模式:', acupoint.name)
    this.setData({
      is3DMode: true,
      selectedAcupoint: acupoint
    })
    
    wx.showToast({
      title: '已进入3D模式',
      icon: 'success'
    })
  },

  // 退出3D模式
  exit3DMode() {
    this.setData({
      is3DMode: false,
      showControlPanel: false,
      zoomLevel: 1,
      rotationX: 0,
      rotationY: 0
    })
    
    wx.showToast({
      title: '已退出3D模式',
      icon: 'success'
    })
  },

  // 切换视图
  switchView(e) {
    const view = e.currentTarget.dataset.view
    const acupoint = this.data.selectedAcupoint
    
    console.log('切换视图:', view, '当前穴位:', acupoint)
    
    // 如果有选中的穴位，使用穴位的视图图片
    if (acupoint && acupoint.viewImages) {
      this.setData({
        currentView: view,
        currentViewImage: acupoint.viewImages[view]
      })
    } else {
      // 如果没有选中穴位，使用默认的视图图片
      const defaultImages = {
        front: 'http://39.97.48.169:9000/zhongyi/e861db718e094fcab304882b42fdde31.png',
        side: 'http://39.97.48.169:9000/zhongyi/71b0c8210d1443cbb9f3485ca13a0484.png',
        back: 'http://39.97.48.169:9000/zhongyi/67de2162e4624d5fb0f05f7f04ff0a08.png'
      }
      
      this.setData({
        currentView: view,
        currentViewImage: defaultImages[view] || defaultImages.front
      })
    }
  },

  // 放大
  zoomIn() {
    const newZoom = Math.min(this.data.zoomLevel + 0.2, 2)
    this.setData({
      zoomLevel: newZoom
    })
  },

  // 缩小
  zoomOut() {
    const newZoom = Math.max(this.data.zoomLevel - 0.2, 0.5)
    this.setData({
      zoomLevel: newZoom
    })
  },

  // 切换控制面板
  toggleControlPanel() {
    this.setData({
      showControlPanel: !this.data.showControlPanel
    })
  },

  // 选择经络穴位
  selectMeridianPoint(e) {
    const point = e.currentTarget.dataset.point
    console.log('选择经络穴位:', point)
    
    // 查找对应的穴位数据
    const acupoint = this.data.acupoints.find(item => 
      item.name.includes(point.name) || point.name.includes(item.name)
    )
    
    if (acupoint) {
      this.setData({
        selectedAcupoint: acupoint
      })
    }
  },

  // 搜索输入
  onSearchInput(e) {
    const keyword = e.detail.value
    this.setData({
      searchKeyword: keyword
    })
    
    // 如果清空输入框，重置搜索结果
    if (!keyword.trim()) {
      this.setData({
        showSearchResults: false,
        searchResults: [],
        highlightedAcupoint: null
      })
    }
  },

  // 搜索确认（点击搜索按钮或按回车）
  onSearchConfirm() {
    const keyword = this.data.searchKeyword.trim()
    
    if (!keyword) {
      wx.showToast({
        title: '请输入穴位名',
        icon: 'none'
      })
      return
    }
    
    // 执行搜索并显示红点标记
    this.performSearch(keyword)
    
    // 显示搜索成功提示
    wx.showToast({
      title: '搜索完成',
      icon: 'success',
      duration: 1500
    })
  },

  // 执行搜索
  performSearch(keyword) {
    console.log('执行搜索:', keyword)
    
    // 搜索穴位
    const results = this.data.acupoints.filter(acupoint =>
      acupoint.name.includes(keyword) ||
      acupoint.pinyin.includes(keyword.toLowerCase()) ||
      acupoint.meridian.includes(keyword) ||
      acupoint.position.includes(keyword)
    )
    
    // 如果找到精确匹配的穴位，高亮显示
    let highlightedAcupoint = null
    if (results.length > 0) {
      // 优先显示完全匹配的穴位
      const exactMatch = results.find(acupoint => 
        acupoint.name === keyword || 
        acupoint.name.includes(keyword)
      )
      highlightedAcupoint = exactMatch || results[0]
    }
    
    this.setData({
      searchResults: results,
      showSearchResults: results.length > 0,
      highlightedAcupoint: highlightedAcupoint
    })
  },

  // 选择搜索结果
  selectSearchResult(e) {
    const acupoint = e.currentTarget.dataset.acupoint
    this.setData({
      selectedAcupoint: acupoint,
      showSearchResults: false,
      searchKeyword: '',
      highlightedAcupoint: acupoint
    })
    
    // 获取对应经络的穴位列表
    const meridianPoints = this.data.meridianDatabase[acupoint.meridian] || []
    this.setData({
      meridianPoints: meridianPoints
    })
  },

  // 关闭搜索结果
  closeSearchResults() {
    this.setData({
      showSearchResults: false,
      searchKeyword: '',
      highlightedAcupoint: null
    })
  },

  // 图片加载成功
  onImageLoad(e) {
    console.log('图片加载成功:', e)
    console.log('图片尺寸:', e.detail.width, 'x', e.detail.height)
    console.log('当前选中穴位:', this.data.selectedAcupoint)
    if (this.data.selectedAcupoint) {
      console.log('穴位坐标:', this.data.selectedAcupoint.coordinates)
    }
  },

  // 图片加载失败
  onImageError(e) {
    console.error('图片加载失败:', e)
    wx.showToast({
      title: '图片加载失败',
      icon: 'error'
    })
  },

  // 返回上一页
  goBack() {
    wx.navigateBack({
      delta: 1
    })
  },

  // 重置视图
  resetView() {
    this.setData({
      zoomLevel: 1,
      rotationX: 0,
      rotationY: 0,
      is3DMode: false,
      showControlPanel: false,
      selectedAcupoint: null,
      highlightedAcupoint: null,
      searchKeyword: '',
      showSearchResults: false,
      searchResults: []
    })
    
    wx.showToast({
      title: '视图已重置',
      icon: 'success',
      duration: 1500
    })
  }
})